<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{path:'/home'}">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片试图区域 -->
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="showAddCateDialog">添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <tree-table class="treeTable" :data="cateList" :columns="columns" :selection-type="false" :expand-type="false"
                  show-index
                  index-text="#" border :show-row-hover="false">
        <!-- 是否有效 -->
        <template slot="isOk" slot-scope="scope">
          <i class="el-icon-success" v-if="scope.row.cat_deleted===false" style="color: lightgreen;"></i>
          <i class="el-icon-error" v-else style="color: red;"></i>
        </template>
        <!-- 排序 -->
        <template slot="order" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level===0">一级</el-tag>
          <el-tag size="mini" type="success" v-else-if="scope.row.cat_level===1">二级</el-tag>
          <el-tag size="mini" type="warning" v-else>三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="opt">
          <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
        </template>
      </tree-table>
      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page="queryInfo.pagenum" :page-size="queryInfo.pagesize" :total="total"
                     :page-sizes="[3, 5, 10, 15]" layout="total, sizes, prev, pager, next, jumper"></el-pagination>
    </el-card>
    <!-- 添加分类的对话框 -->
    <el-dialog title="添加分类" :visible.sync="addCateDialogVisible" width="50%" @close="addCateDialogClosed">
      <el-form :model="addCateForm" :rules="addCateFormRules" ref="addCateFormRef" label-width="100px">
        <el-form-item label="分类名称：" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类：">
          <!-- Options指定数据源 -->
          <!-- Props指定配置对象 -->
          <el-cascader expand-trigger="hover" :options="parentCateList" :props="cascaderProps"
                       v-model="selectKeys" @change="parentCateChanged" clearable change-on-select></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible=false">取消</el-button>
        <el-button type="primary" @click="addCate">添加</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
  export default {
    data () {
      return {
        queryInfo: { type: 3, pagenum: 1, pagesize: 5 },
        // 商品分类的数据列表，默认为空
        cateList: [],
        // 总条数
        total: 0,
        // 为table指定列的定义
        columns: [{ label: '分类名称', prop: 'cat_name' },
          // type:'template'表示将当前列定义为模版列 template:'isOk' 表示当前列使用模版名称
          { label: '是否有效', type: 'template', template: 'isOk' },
          { label: '排序', type: 'template', template: 'order' },
          { label: '操作', type: 'template', template: 'opt' }],
        // 添加分类对话框显示开关
        addCateDialogVisible: false,
        // 添加分类的表单：cat_pid 父级分类Id、cat_level 分类等级 默认一级分类
        addCateForm: { cat_name: '', cat_pid: 0, cat_level: 0 },
        // 添加分类表单的验证规则对象
        addCateFormRules: { cat_name: [{ required: true, message: '清输入分类名称', trigger: 'blur' }] },
        // 父级分类的列表
        parentCateList: {},
        // 指定级联选择器的配置对象
        cascaderProps: { value: 'cat_id', label: 'cat_name', children: 'children' },
        // 选中的父级分类的ID数组
        selectKeys: []
      }
    },
    created () {
      this.getCateList()
    },
    methods: {
      async getCateList () {
        const { data: res } = await this.$http.get('categories', { params: this.queryInfo })
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.cateList = res.data.result
        this.total = res.data.total
      },
      // 监听 pageSize 改变事件
      handleSizeChange (newSize) {
        this.queryInfo.pagesize = newSize
        this.getCateList()
      },
      // 监听 pagenum 改变事件
      handleCurrentChange (newPage) {
        this.queryInfo.pagenum = newPage
        this.getCateList()
      },
      // 点击按钮，展示添加分类的对话框
      showAddCateDialog () {
        this.getParentCateList()
        this.addCateDialogVisible = true
      },
      // 获取父级分类的数据列表
      async getParentCateList () {
        const { data: res } = await this.$http.get('categories', { params: { type: 2 } })
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.parentCateList = res.data
      },
      // 选择项发生变化的监听函数
      parentCateChanged () {
        // 如果selectKeys数组中length大于0，证明选中父级分类
        // 反之说明没有选中任何父级分类
        if (this.selectKeys.length > 0) {
          // 设置父级分类id
          this.addCateForm.cat_pid = this.selectKeys[this.selectKeys.length - 1]
          // 设置当前分类的等级
          this.addCateForm.cat_level = this.selectKeys.length
        } else {
          this.addCateForm.cat_pid = 0
          this.addCateForm.cat_level = 0
        }
      },
      addCate () {
        this.$refs.addCateFormRef.validate(async valid => {
          if (!valid) return
          const { data: res } = await this.$http.post('categories', this.addCateForm)
          if (res.meta.status !== 201) return this.$message.error(res.meta.msg)
          this.$message.success(res.meta.msg)
          this.getCateList()
          this.addCateDialogVisible = false
        })
      },
      addCateDialogClosed () {
        this.$refs.addCateFormRef.resetFields()
        this.selectKeys = []
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      }
    }
  }
</script>
<style lang="less" scoped>
  .treeTable {
    margin-top: 15px;
  }

  .el-cascader {
    width: 100%;
  }
</style>
